package org.hilo.boot.core.util;

import org.jretty.util.ExceptionUtils.StackTraceFilter;

/**
 * 异常堆栈信息简化工具
 * 
 * @author zollty
 * @since 2020年11月2日
 */
public class HiloStackTraceFilter implements StackTraceFilter {
    
    private HiloStackTraceFilter() {}
    
    // 贫血单例模式
    private static class LazyHolder {
        private static final HiloStackTraceFilter INSTANCE = new HiloStackTraceFilter();
    }

    public static HiloStackTraceFilter me() {
        return LazyHolder.INSTANCE;
    }

    @Override
    public boolean exclude(String className) {
        return !includeApp(className) && (
                excludeSpServlet(className) 
                || excludeSpFilter(className) 
                || excludeSpAOP(className)
                || excludeTomcat(className) 
                || excludeJava(className) 
                || excludeDubbo(className)
                || excludeShiro(className));
    }
    
    private static boolean includeApp(String className) {
        return className.startsWith("org.hilo.boot") 
                || className.startsWith("com.zollty");
    }
    
    private static boolean excludeJava(String className) {
        return className.startsWith("sun.reflect");
    }

    private static boolean excludeTomcat(String className) {
        return className.startsWith("org.apache.catalina")
                || className.startsWith("org.apache.coyote");
    }

    private static boolean excludeSpAOP(String className) {
        return className.startsWith("org.springframework.aop");
    }
    
    private static boolean excludeSpServlet(String className) {
        return className.startsWith("org.springframework.web.servlet");
    }
    
    private static boolean excludeSpFilter(String className) {
        return className.startsWith("org.springframework.web.filter");
    }
    
    private static boolean excludeDubbo(String className) {
        return className.startsWith("com.alibaba.dubbo");
    }
    
    private static boolean excludeShiro(String className) {
        return className.startsWith("org.apache.shiro");
    }
}
